/**
* Created by ren on 2017/1/6.
*/
<template>
  <div class='updatePayPwd'>
    <div class="item"><label>旧支付密码：</label><input type="password" v-model="oldPassword"></div>
    <div class="item"><label>新支付密码：</label><input type="password" v-model="newPassword"></div>
    <div class="item"><label>确认新支付密码：</label><input type="password" v-model="confirmPassword"></div>
    <div class="btns" style="text-align: center">
      <button class="btn" :disabled="!btnStatus" @click="updatePayPwd">确认修改</button>
    </div>
    <Notify :parentThis='this'></Notify>
  </div>
</template>

<script>
  import API from '../../api'
  import Notify from '../Notify'
  import md5 from 'blueimp-md5'
  export default {
    name: 'updatePayPwd',
    components: {
      Notify
    },
    data: function () {
      return {
        notifyStatus: 2,
        oldPassword: '',
        newPassword: '',
        confirmPassword: '',
        btnStatus: false
      }
    },
    props: ['_parentThis'],
    methods: {
      updatePayPwd: function () {
        this.$http.post(API.updatePaymentPassword, {
          id: this.$cookie.get('id'),
          oldPassword: md5('Au+' + (this.oldPassword * 103)),
          newPassword: md5('Au+' + (this.newPassword * 103)),
          confirmPassword: md5('Au+' + (this.confirmPassword * 103))
        }).then((ret) => {
          const result = ret.body
          if (result.code === 1) {
            this._parentThis.safeChild = 'parent'
          } else {
            this.notifyStatus = '0|' + result.message
          }
        }, (err) => {
          console.log(err)
        })
      }
    },
    watch: {
      newPassword: function (newVal) {
        if (/^\d{6}$/.test(newVal)) {
          if (newVal === this.confirmPassword && this.oldPassword !== '' && newVal !== '') {
            this.btnStatus = true
          } else {
            this.btnStatus = false
          }
        } else {
          this.notifyStatus = '0|新密码必须为六位数字！'
        }
      },
      confirmPassword: function (newVal) {
        if (/^\d{6}$/.test(newVal)) {
          if (newVal === this.newPassword && this.oldPassword !== '' && newVal !== '') {
            this.btnStatus = true
          } else {
            this.btnStatus = false
          }
        } else {
          this.notifyStatus = '0|新密码必须为六位数字！'
        }
      },
      oldPassword: function (newVal) {
        if (this.confirmPassword === this.newPassword && this.newPassword !== '' && newVal !== '') {
          this.btnStatus = true
        } else {
          this.btnStatus = false
        }
      }
    }
  }
</script>
